package com.aiyige.page.selectinterest;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.aiyige.R;
import com.aiyige.base.eventbus.EventInterestSelectedAllCleared;
import com.aiyige.base.eventbus.EventInterestSelectedStateChanged;
import com.aiyige.page.selectinterest.adapter.StateAdapter;
import com.aiyige.page.selectinterest.model.IndustryItem;
import com.aiyige.page.selectinterest.model.InterestItem;
import com.aiyige.page.selectinterest.model.StateItem;
import com.chad.library.adapter.base.BaseQuickAdapter;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.LinkedList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;


public class StatePage extends Fragment {

    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    Unbinder unbinder;
    StateAdapter stateAdapter;
    List<IndustryItem> data;

    public StatePage() {

    }

    public List<IndustryItem> getData() {
        return data;
    }

    public void setData(List<IndustryItem> data) {
        this.data = data;
    }

    public static StatePage newInstance(List<IndustryItem> data) {
        StatePage fragment = new StatePage();
        fragment.setData(data);
        return fragment;
    }


    @Override
    public View onCreateView(final LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.page_state, container, false);
        unbinder = ButterKnife.bind(this, view);
        stateAdapter = new StateAdapter();
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        recyclerView.setAdapter(stateAdapter);
        stateAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                switch (stateAdapter.getItem(position).getType()) {
                    case StateItem.TYPE_CLEAN: {
                        for (IndustryItem item : data) {
                            if (item.isStats()) continue;
                            item.clearAllInterestSelectedState();
                        }
                        break;
                    }
                    case StateItem.TYPE_ALL_INTEREST: {
                        for (IndustryItem item : data) {
                            if (item.isStats()) continue;
                            if (item.getId().equals(stateAdapter.getItem(position).getParentId())) {
                                item.clearAllInterestSelectedState();
                            }
                        }
                        break;
                    }
                    case StateItem.TYPE_INTEREST: {
                        for (IndustryItem item : data) {
                            if (item.isStats()) continue;
                            if (item.getId().equals(stateAdapter.getItem(position).getParentId())) {
                                item.clearInterestSelectedStateById(stateAdapter.getItem(position).getId());
                            }
                        }
                        break;
                    }
                    default:
                        break;
                }
                EventBus.getDefault().post(new EventInterestSelectedAllCleared());
                doRefresh();
            }
        });
        doRefresh();
        EventBus.getDefault().register(this);
        return view;
    }


    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventInterestSelectedStateChanged(EventInterestSelectedStateChanged event) {
        doRefresh();
    }

    public void doRefresh() {
        List<StateItem> stateItemList = new LinkedList<>();
        stateItemList.add(StateItem.CLEAN);
        for (IndustryItem item : data) {
            if (!item.isStats()) {
                List<InterestItem> interestItemList = item.getSelectedInterestItemList();
                for (InterestItem interestItem : interestItemList) {
                    if (interestItem.isAllSelected()) {
                        stateItemList.add(new StateItem("-1", item.getId(), item.getTitle(), StateItem.TYPE_ALL_INTEREST));
                        break;
                    }
                    stateItemList.add(new StateItem(interestItem.getId(), item.getId(), interestItem.getTitle(), StateItem.TYPE_INTEREST));
                }
            }
        }
        stateAdapter.setNewData(stateItemList);
    }


    @Override
    public void onDestroyView() {
        EventBus.getDefault().unregister(this);
        super.onDestroyView();
        unbinder.unbind();
    }
}
